package com.nf.health.app.adapter;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.nf.health.app.R;
import com.nf.health.app.activity.global.PictureActivity;
import com.nf.health.app.glide.GlideUtil;
import com.nf.health.app.sweat.bean.SweatHistoryDataBean;
import com.nf.health.app.utils.ActivityUtils;

import java.util.ArrayList;

/**
 * Created by 恩福 on 2016/11/22.
 *
 * @auther changpeng
 * @email obj_cp@126.com
 */

public class ImageViewAdapter extends RecyclerView.Adapter<ImageViewAdapter.MyViewHolder> {


    private SweatHistoryDataBean.DeviceDataBean deviceDataBean;
    private Context context;
    private String[] imageUrls;

    public ImageViewAdapter(Context context, SweatHistoryDataBean.DeviceDataBean deviceDataBean) {
        this.context = context;
        this.deviceDataBean = deviceDataBean;
        initImageUrl();
    }

    private void initImageUrl() {
        imageUrls = new String[]{
                deviceDataBean.getSweaturl01(),
                deviceDataBean.getSweaturl02(),
                deviceDataBean.getSweaturl03(),
                deviceDataBean.getSweaturl04(),
                deviceDataBean.getSweaturl05(),
                deviceDataBean.getSweaturl06(),
                deviceDataBean.getSweaturl07(),
                deviceDataBean.getSweaturl08(),
                deviceDataBean.getSweaturl09(),
                deviceDataBean.getSweaturl10(),
                deviceDataBean.getSweaturl11(),
                deviceDataBean.getSweaturl12(),
                deviceDataBean.getSweaturl13(),
                deviceDataBean.getSweaturl14(),
                deviceDataBean.getSweaturl15(),
                deviceDataBean.getSweaturl16(),
                deviceDataBean.getSweaturl17(),
                deviceDataBean.getSweaturl18(),
                deviceDataBean.getSweaturl19(),
                deviceDataBean.getSweaturl20(),
        };
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_imageview, null);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        GlideUtil.loadNetImg2(context, holder.image, imageUrls[position], R.drawable.default_square_image);
        holder.image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                ArrayList<String> urls = new ArrayList<String>();
                for (String imageUrl : imageUrls) {
                    urls.add(imageUrl);
                }
                bundle.putStringArrayList("imgUrls",urls);
                bundle.putInt("position", position);
                bundle.putInt("type", PictureActivity.TYPE_NET_IMG);
                ActivityUtils.showActivity(context, PictureActivity.class, bundle);
            }
        });

    }


    @Override
    public int getItemCount() {
        return deviceDataBean == null ? 0 : 20;
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView image;

        public MyViewHolder(View itemView) {
            super(itemView);
            image = (ImageView) itemView.findViewById(R.id.sweat_recycler_image);
        }
    }
}
